---
sidebar_position: 92
description: How to configure a MongoDB retriever.
---
import { integrations } from "@site/data/integrations";
import {Mandatory, NotMandatory} from "@site/src/components/checks/checks";
export const retrieverName = 'MongoDB';
export const info = integrations.retrievers.find((r) => r.name === retrieverName)

# MongoDB

## Overview
{info.longDescription ?? info.description}

## MongoDB Collection Format
If you use MongoDB to store your flags, you need a specific format to store your flags.

We expect the flag to be stored in JSON format as defined in the [create flags section](../../configure_flag/create-flags),
but you should also add a new field called `flag` containing the name of the flag.

The retriever will read all the flags from the collection at once.

#### Example
```json title="mongoDB flag format"
{
    // highlight-next-line
    "flag": "new-admin-access",
    "variations": {
        "default_var": false,
        "false_var": false,
        "true_var": true
    },
    "defaultRule": {
        "percentage": {
            "false_var": 70,
            "true_var": 30
        }
    }
}
```

## Configure the relay proxy

To configure your relay proxy to use the {retrieverName} retriever, you need to add the following
configuration to your relay proxy configuration file:

```yaml title="goff-proxy.yaml"
# ...
retrievers:
  - kind: mongodb
    uri: mongodb://root:example@127.0.0.1:27017/
    database: appConfig
    collection: featureFlags
# ...
```
| Field name   |   Mandatory   | Type   | Default  | Description                                                                                               |
|--------------|:-------------:|--------|----------|-----------------------------------------------------------------------------------------------------------|
| `kind`       | <Mandatory /> | string | **none** | Value should be **`mongodb`**.<br/>_This field is mandatory and describes which retriever you are using._ |
| `uri`        | <Mandatory /> | string | **none** | This is the MongoDB URI used in order to connect to the MongoDB instance.                                 |
| `database`   | <Mandatory /> | string | **none** | Name of the **database** where flags are stored.                                                          |
| `collection` | <Mandatory /> | string | **none** | Name of the **collection** where flags are stored.                                                        |


## Configure the GO Module
To configure your GO module to use the {retrieverName} retriever, you need to add the following
configuration to your `ffclient.Config{}` object:

```go title="example.go"
err := ffclient.Init(ffclient.Config{
  PollingInterval: 3 * time.Second,
  Retriever: &mongodbretriever.Retriever{
    Collection: "featureFlags",
    Database:   "appConfig",
    URI:        "mongodb://root:example@127.0.0.1:27017/",
  },
})
defer ffclient.Close()
```


| Field            |   Mandatory   | Description                                                 |
|------------------|:-------------:|-------------------------------------------------------------|
| **`Collection`** | <Mandatory /> | Name of the collection where your flags are stored          |
| **`Database`**   | <Mandatory /> | Name of the mongo database where the collection is located. |
| **`URI`**        | <Mandatory /> | Connection URI of your mongoDB instance.                    |
